package com.study.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.study.gift.client.AuthClient;
import com.study.gift.domain.Employee;
import com.study.gift.domain.LoginUser;
import com.study.gift.domain.Tenant;
import com.study.gift.exception.ErrorCode;
import com.study.gift.exception.XizhouAssert;
import com.study.gift.mapper.EmployeeMapper;
import com.study.gift.mapper.TenantMapper;
import com.study.gift.query.TenantQuery;
import com.study.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.study.gift.util.AjaxResult;
import com.study.gift.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xizhou
 * @since 2023-04-24
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AuthClient authClient;

    @Override
    public PageList selectPageList(TenantQuery query) {
        Page<Tenant> page = new Page<>(query.getPage(),query.getRows());
        //传入分页对象,查询完成后,把总结放到分页对象中,并且响应当前页数据给我
        List<Tenant> tenants=  tenantMapper.loadPageList(page,query);
        return new PageList(page.getTotal(),tenants);
    }

    @Override
    public AjaxResult settlement(Tenant tenant) {
        validate(tenant);

        //2 百度审核  @TODO
        boolean auditResult = true;
        String message = "审核通过!";


        //3 远程调用授权中心保存LoginUser并返回loginId
        LoginUser loginUser = new LoginUser();
        Employee employee = tenant.getEmployee();
        loginUser.setUsername(employee.getUsername());
        loginUser.setPassword(employee.getPassword());
        loginUser.setEmail(employee.getEmail());
        loginUser.setTel(employee.getTel());
        loginUser.setType(0);

        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if (!ajaxResult.isSuccess()){
            return ajaxResult;
        }
        // ??????
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        //4 保存租户管理员 loginId
        employee.setRealName(employee.getUsername());
        employee.setInputTime(new Date());
        employee.setState(0);
        employee.setType(1); //0管理员  1店铺管理员 3店员
        employee.setLoginId(loginId);
        employeeMapper.insert(employee);
        //5 保存租户 admin_id
        tenant.setEmployee(employee);
        tenant.setAdminId(employee.getId());
        tenant.setRegisterTime(new Date());
        tenant.setState(auditResult?1:2);
        tenantMapper.insert(tenant);
        //6 租户和套餐中间表
        tenantMapper.saveTenantMeal(tenant.getId(),tenant.getMealId());
        //7 添加审核日志 @TODO
        //8 发邮件激活账号 @TODO
        return AjaxResult.me();
    }


    //3 断言版本
    private void validate(Tenant tenant) {
        //1.1 null校验-暂时不做
//        XizhouAssert.isNotNull(tenant,"kkk");
//        XizhouAssert.isNotNull(tenant.getAdmin(),"kkk");
//        XizhouAssert.isNotNull(tenant.getMealId(),"kkk");
        //1.2 两次密码
        Employee admin = tenant.getEmployee();
        String password = admin.getPassword();
        String confirmPassword = admin.getConfirmPassword();
        XizhouAssert.isEq(password,confirmPassword, ErrorCode.CODE_400_TENANT_ADMIN_PASSWORD_NOT_EQ);
        //1.3 店铺名重复 哪怕查不到返回不是null是null集合(new ArrayList();)
        List<Tenant> tenants = tenantMapper.selectList(new EntityWrapper<Tenant>()
                .eq("company_name", tenant.getCompanyName()));
        XizhouAssert.isNull(tenants, ErrorCode.CODE_400_TENANT_EXSIT);
        //1.4 管理员是否存在
        String username = admin.getUsername();
        List<Employee> employees = employeeMapper.selectList(new EntityWrapper<Employee>()
                .eq("username", username));
        XizhouAssert.isNull(employees,ErrorCode.CODE_400_TENANT_ADMIN_EXSIT);
    }
}
